#!/usr/bin/perl -w
##############################################################################
#
# test_Exception.pl
# =================
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   test_Exception demonstrates the traceability of PerlAutoTest framework in
#   the event of run time error.
#
# Steps:
#
#   1.) Instantiate a CiscoRouter object and call it C2600
#   2.) Start a telnet session to target host
#   3.) Log the entire telnet session to a file
#   4.) Login to the target host
#   5.) Execute Cisco 2600 router's "show ip route" command
#   6.) Parse the command output and extra out the default route info
#   7.) Release C2600's resource
#
# Expected result:
#
#   Telnet to cisco router and perform the show ip route command and exit cleanly.
#
##############################################################################

package main;
$VERSION = 1.01;

use AutoTest::Exceptions;

# Set script variables
my $MySetup = undef;

# Try block
eval {
  # Check if environment variables are set
  if (!defined $ENV{'PAT_ROOT'}) {
	AutoTest::Exception::Environment->throw('PAT_ROOT environment variable not set');
  }
  if (!defined $ENV{'PAT_LOG'}) {
	AutoTest::Exception::Environment->throw('PAT_LOG environment variable not set');
  }
  if (!defined $ENV{'PERLLIB'}) {
	AutoTest::Exception::Environment->throw('PERLLIB environment variable not set');
  }

  use Setup_B;

  # Instantiate a test Setup_B object and call it MySetup
  $MySetup = Setup_B->new();

  # Start a telnet session to the C2600 cisco router installed in Setup_B
  $MySetup->{C2600}->openTelnet();

  # Log the entire telnet session to a file
  $MySetup->{C2600}->log($ENV{'PAT_LOG'} . "test_Exception.log");

  $MySetup->{C2600}->logMessage("\nLogin to the target host\n");
  $MySetup->{C2600}->login(undef, qr/Password: $/, qr/>/, qr/#/);

  $MySetup->{C2600}->logMessage("\nExecute Cisco 2600 router show ip route command\n");
  $myRoute = $MySetup->{C2600}->showIPRoute();

  $MySetup->{C2600}->logMessage("\nParse the command output and extra out the default route info\n");
  @lines = split ( /\n/, $myRoute);
  foreach $line (@lines) {
	if ($line =~ /S\*/) {
	    $line =~ m{.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).* (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})};
	    # $1 holds the first match and $2 holds the second match
	    $MySetup->{C2600}->logMessage("\nDefault distination network: $1\n");
	    $MySetup->{C2600}->logMessage("\nDefault gateway: $2\n");
	    last;
	}
  }

  # Stop logging telnet session output
  $MySetup->{C2600}->stopLog();

  # Release test setup resouce
  $MySetup->{C2600}->DESTROY();

	#############################	
	# Put test report code here #
	#############################
};

# Catch block
if ($@) {
  if ( !UNIVERSAL::isa( $@, 'AutoTest::Exception::Connection' ) ) {
  	# Stop logging telnet session output
  	$MySetup->{C2600}->stopLog();
	
	# Release test setup resouce
	$MySetup->{C2600}->DESTROY();
  }
  if ( UNIVERSAL::isa( $@, 'AutoTest::Exception' ) ) {
	############################################################################	
	# Put test report code here.  Also remember to inset stack trace in report #
	############################################################################
  	warn $@->error, "\n", $@->trace->as_string, "\n";
  	warn join ' ',  $@->package, $@->euid, $@->egid, $@->uid, $@->gid, $@->pid, $@->time;

  } else {
	# Not a error from Perl Autotest Framework
	ref $@ ? $@->rethrow : die $@;
  }
}
